<?php
namespace Home\Controller;
use Think\Controller;

class FenController extends BaseController{

	public function index(){
		$this->display();
	}
	
	public function xiaindex(){
		$this->display();
	}
	
	
	public function cardpage(){
		$uid = session('user')?session('user')['id']:'';
		$info = array();
		$info = M('card')->where('uid='.$uid.' and status=1')->find();
		if(!empty($info)){
		    $u = M('user')->field('points')->find();
		    $info['points'] = $u['points'];
		    $this->assign('info',$info);
		    $this->display();
		}else{
			$this->display('User/tkInfocard');
		}
	}
	
	public function bindpage(){
	    $this->display();
	}
	
	public function addcard(){
	    $data = I();
	    $user = session('user');
	    $user_id = $user['id'];
	    
	   // if(isset($data['bank'])) $this->error('开户行不能为空!');
	   // if(isset($data['bank_name'])) $this->error('开户行不能为空!');
	   $res = M('user')->where('id='.$user_id)->save($data);
	    
	    
	    $this->success('绑卡成功!',U('Home/Run/index'),1);
	}
	
	public function addpage(){
		if (C('agent_pay') == '1') {
			$user = session('user');
			if ($user['t_id']) {
				$agentinfo = M('user')->where("id = {$user['t_id']}")->find();
				if ($agentinfo['t_id']) {
					$agentinfo = M('user')->where("id = {$agentinfo['t_id']}")->find();
					if ($agentinfo['t_id']) {
						$agentinfo = M('user')->where("id = {$agentinfo['t_id']}")->find();
					}
				}
				$pay = $agentinfo;
			} else {
				$info = M('config')->where("id = 2")->find();
				$pay['wx_paycode'] = $info['kefu'];
				$info1 = M('config')->where("id = 3")->find();
				$pay['zfb_paycode'] = $info1['kefu'];
			}
		} else {
			$info = M('config')->where("id = 2")->find();
			$pay['wx_paycode'] = $info['kefu'];
			$info1 = M('config')->where("id = 3")->find();
			$pay['zfb_paycode'] = $info1['kefu'];
		}
		$this->assign('pay',$pay);
		$this->display();
	}

	public function xiapage(){
		$uid = session('user')?session('user')['id']:'';
		$info = array();
		$info = M('user')->field('*')->where('id='.$uid)->find();
		if($info['bank']){
			$this->assign('info',$info);
			$this->display();
		}else{
			$this->display('User/tkInfo1');
		}
	}

	protected function send($content){
		// 指明给谁推送，为空表示向所有在线用户推送
		$to_uid = "";
		// 推送的url地址，上线时改成自己的服务器地址
		$push_api_url = "http://localhost:2128/";
		$post_data = array(
		   "type" => "publish",
		   "content" => json_encode($content),
		   "to" => $to_uid, 
		);
		$ch = curl_init ();
		curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
		curl_setopt ( $ch, CURLOPT_POST, 1 );
		curl_setopt ( $ch, CURLOPT_HEADER, 0 );
		curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
		curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Expect:"));
		$return = curl_exec ( $ch );
		curl_close ( $ch );
		return $return;
	}

	public function add(){
		$user = session('user');
		$money = I('money');
		$usdt_type = I('usdt_type');
		$usdt_address = I('usdt_address');
		$userinfo = M('user')->where("id=".$user['id'])->find();
		
		if($money>$userinfo['points']) $this->error("余额不足", '', true);
		
		$add = array(
			'uid' => $id,
			't_id' => $userinfo['t_id'],
			'money' => $money,
			'balance' => $userinfo['points'],
			'nickname' => $userinfo['nickname'],
			'headimgurl' => $userinfo['headimgurl'],
			'addtime' => time(),
		);
		
		$res = M('fenadd')->add($add);
		if ($res) {
			$message  = array(
				'time'=>date('H:i:s'),
				'type' => 1,
				'content'=>"上分申请"
			);
// 			$res = $this->send($message);
			$this->success('The application is successful, waiting for approval, and is being transferred.',U('Home/Run/index'),1);
		} else {
			$this->error('Application failed, please contact customer service');
		}
	}

/**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * money:1
    type:1
    bank_account:1
    back_address:11
    back_name:1
    password:123456s
    uid:13
    Name

     */
	public function xia(){
		$data = I('post.');
		$uid = session('user')?session('user')['id']:'';
		$userinfo = M('user')->where("id=$uid")->find();
		if($userinfo['bank']==''){
            $this->error('You have not set up the withdrawal information, please go to the personal center to set it up.',U('home/user/index'),0);
		}
		/*if(md5($data['password'])!=$userinfo['tk_password']){
			$this->error('The withdrawal password you entered is incorrect. Please re-enter it.');
		}*/
		  
		$data['balance'] = $userinfo['points'];
		$data['t_id'] = $userinfo['t_id'];
		$data['nickname'] = $userinfo['nickname'];
		$data['headimgurl'] = $userinfo['headimgurl'];
		$data['addtime'] = time();
		 unset($data['password']);
		$res = M('fenxia')->add($data);
		if ($res) {
			$message  = array(
				'time'=>date('H:i:s'),
				'type' => 2,
				'content'=>"Apply for lower points"
			);
			$res = $this->send($message);
			$this->success('The application is successful, waiting for approval, and is being transferred.',U('Home/Run/index'),1);
		} else {
			$this->error('Application failed, please contact customer service');
		}
	}
}
?>